package cn.zhiwen.xmn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ThreadConsole {
    //线程死循环
    public static void createBusinessThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                }
            }
        }, "createBusinessThread").start();
    }

    //线程锁等待演示
    public static void createLockWait(Object lock) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "createLockWait").start();
    }

    //死锁的生成
    static class SimpleDieLock implements Runnable {
        Integer a;
        Integer b;

        public SimpleDieLock(Integer a, Integer b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            synchronized (Integer.valueOf(a)){
                synchronized (Integer.valueOf(b)){
                    System.out.println(a+b);
                }
            }
        }
    }

    static class SimpleDiesLock implements Runnable {
        private boolean lockFlag;
        static Object o1 = new Object();
        static Object o2 = new Object();

        public SimpleDiesLock(boolean lockFlag) {
            this.lockFlag = lockFlag;
        }

        @Override
        public void run() {
            if(lockFlag){
                synchronized (o1){
                    try {
                        System.out.println("lock1");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (o2){
                        System.out.println("lock1");
                    }
                }
            }else{
                synchronized (o2){
                    try {
                        System.out.println("lock2");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (o1){
                        System.out.println("lock2");
                    }
                }
            }

        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        bufferedReader.readLine();
        createBusinessThread();
        bufferedReader.readLine();
        Object obj = new Object();
        createLockWait(obj);
        bufferedReader.readLine();
        for (int i = 0; i < 100; i++) {
            new Thread(new SimpleDieLock(1,2)).start();
            new Thread(new SimpleDieLock(2,1)).start();
        }
//        new Thread(new SimpleDiesLock(true)).start();
//        new Thread(new SimpleDiesLock(false)).start();
    }


}
